package org.example.pingkunsheng.service.impl;

import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.example.core.exception.ErrorException;
import org.example.core.service.impl.SimpleServiceImpl;
import org.example.core.utils.DocumentUtil;
import org.example.pingkunsheng.entity.TTuitionWaiverEntity;
import org.example.pingkunsheng.model.TTuitionWaiverModel;
import org.example.pingkunsheng.service.TTuitionWaiverService;
import org.springframework.context.annotation.Primary;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Description:
 * Date: 2024/1/31 22:09
 */
@Service
@Primary
public class TTuitionWaiverServiceImpl extends SimpleServiceImpl<String, TTuitionWaiverModel, TTuitionWaiverEntity> implements TTuitionWaiverService {
    @Override
    public void exportList(Map<String, Object> params, HttpServletResponse response) {
        ServletOutputStream outputStream = null;
        try {
            outputStream = response.getOutputStream();
        } catch (IOException e) {
            throw new ErrorException(e);
        }
        XSSFWorkbook workbook = new XSSFWorkbook();
        XSSFSheet sheet = workbook.createSheet("学费减免统计报表");
        List<TTuitionWaiverModel> tTuitionWaiverModelList = searchAll();
        XSSFRow header = sheet.createRow(0);
        header.createCell(0).setCellValue("姓名");
        header.createCell(1).setCellValue("性别");
        header.createCell(2).setCellValue("出生年月");
        header.createCell(3).setCellValue("入学时间");
        header.createCell(4).setCellValue("学号");
        header.createCell(5).setCellValue("年级班级");
        header.createCell(6).setCellValue("身份证号码");
        header.createCell(7).setCellValue("审核状态");

        for (int index = 1; index < tTuitionWaiverModelList.size(); index++) {
            TTuitionWaiverModel tTuitionWaiverModel = tTuitionWaiverModelList.get(index - 1);
            XSSFRow row = sheet.createRow(index);
            XSSFCell nameCell = row.createCell(0);
            nameCell.setCellValue(tTuitionWaiverModel.getStudentName());
            XSSFCell sexCell = row.createCell(1);
            sexCell.setCellValue(tTuitionWaiverModel.getStudentSex());
            XSSFCell birthCell = row.createCell(2);
            birthCell.setCellValue(tTuitionWaiverModel.getStudentBirth());
            XSSFCell intakeCell = row.createCell(3);
            intakeCell.setCellValue(tTuitionWaiverModel.getStudentIntake());
            XSSFCell codeCell = row.createCell(4);
            codeCell.setCellValue(tTuitionWaiverModel.getStudentCode());
            XSSFCell classCell = row.createCell(5);
            classCell.setCellValue(tTuitionWaiverModel.getStudentClass());
            XSSFCell idCardCell = row.createCell(6);
            idCardCell.setCellValue(tTuitionWaiverModel.getStudentIdCard());
            XSSFCell statusCell = row.createCell(7);
            switch (tTuitionWaiverModel.getStatus()) {
                case "0":
                    statusCell.setCellValue("待提交");
                    break;
                case "1":
                    statusCell.setCellValue("待审核");
                    break;
                case "2":
                    statusCell.setCellValue("审核中");
                    break;
                case "3":
                    statusCell.setCellValue("已审核");
                    break;
                case "4":
                    statusCell.setCellValue("驳回");
                    break;
            }
        }
        try {
            DocumentUtil.response(response,workbook,"贫困减免申请.xlsx");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void updateStatus(Map<String, Object> params) {
        String id = String.valueOf(params.get("id"));
        TTuitionWaiverModel tuitionWaiverModel = getById(id);
        tuitionWaiverModel.setStatus(String.valueOf(params.get("status")));
        update(tuitionWaiverModel);
    }
}
